<?php

namespace App\Game;

use App\Game\Game;
use Carbon\Carbon;
use App\Game\Traits\PtsTrait;

class pts extends Game
{
    use PtsTrait;

    public $timezone = 'UTC';
    private $platName = 'pts';


    const LOGIN = '/v1/login';
    const CREATE = '/v1/players';
    const TRANSFERIN = '/v1/payments/transfers/in';
    const TRANSFEROUT = '/v1/payments/transfers/out';
    const BALANCE = '/v1/players/';
    const RECORD = '/v1/history/game';

    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }



    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $this->initHeader();
        $demo = $request->input('demo', '');
        $url = $this->gameUrl . self::CREATE;
        $isTest = false;
        if (!empty($demo)) {
            $isTest = true;
        }
        $customData = [
            'key' => 'league',
            'keyName' => 'faction',
            'value' => 'Noasauridae'
        ];

        $data = [
            'json' => [
                'code' => $accountUserName,
                'firstName' => 'Name',
                'lastName' => 'Surname',
                'email' => $accountUserName . '@email.email',
                'country' => 'CN',
                'currency' => 'CNY',
                'isTest' => $isTest,
                'language' => 'zh-cn',
                'status' => 'normal',
                'password' => $password,
                'customData' => $customData
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['status'])) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }


    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $this->initHeader();
        $url = $this->gameUrl . self::BALANCE . $accountUserName;
        $output = $this->request('GET', $url, [], $this->header);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['balances'])) {
            return ['success' => true, 'data' => ['balance' => $outputarr['balances']['CNY']['main']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '用户不存在'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $this->initHeader();
        $url = $this->gameUrl . self::TRANSFERIN;
        $data = [
            'json' => [
                'playerCode' => $accountUserName,
                'currency' => 'CNY',
                'amount' => $price,
                'extTrxId' => $transferid
            ]
        ];
        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['orderStatus'])) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else if (isset($outputarr['code'])) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $this->initHeader();
        $url = $this->gameUrl . self::TRANSFEROUT;
        $data = [
            'json' => [
                'playerCode' => $accountUserName,
                'currency' => 'CNY',
                'amount' => $price,
                'extTrxId' => $transferid
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['orderStatus'])) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else if (isset($outputarr['code'])) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $this->initHeader();
        $url = $this->gameUrl . '/v1/payments/' . $request->transferid;
        $data = [
            'operationType' => 'transfer',
        ];

        $output = $this->request('GET', $url, $data, $this->header);
        $outputarr = json_decode($output, true);

        if ($outputarr['status'] == 'committed') {
            return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '查询转账失败'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    //进入游戏
    public function curlJoinGame($request)
    {
        $this->initHeader();
        $accountUserName = $request->input('accountUserName');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');
        $url = $this->gameUrl . '/v1/players/' . $accountUserName . '/games/' . $gamecode;
        $mode = empty($demo) ? 'real' : 'demo';
        $data = [
            'playmode' => $mode,
            'language' => 'zh-cn'
        ];

        $output = $this->request('GET', $url, $data, $this->header);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['url'])) {
            return ['success' => true, 'data' => ['url' => $outputarr['url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }


    public function curlJoinMobileGame($request)
    {
        $this->initHeader();
        $accountUserName = $request->input('accountUserName');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');
        $url = $this->gameUrl . '/v1/players/' . $accountUserName . '/games/' . $gamecode;
        $mode = empty($demo) ? 'real' : 'demo';
        $data = [
            'playmode' => $mode,
            'language' => 'zh-cn'
        ];

        $output = $this->request('GET', $url, $data, $this->header);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['url'])) {
            return ['success' => true, 'data' => ['url' => $outputarr['url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $page = 1;
        $limit = 1000;
        $this->initHeader();
        $url = $this->gameUrl . self::RECORD;
        $start_time = $startTime->format('Y-m-d\TH:i:s\Z');
        $end_time = $endTime->format('Y-m-d\TH:i:s\Z');

        do {
            $flag = false;
            $params = [
                'offset' => ($page - 1) * $limit,
                'finished' => true,
                'ts__gt' => $start_time,
                'ts__lte' => $end_time,
                'limit' => $limit,
                'sortBy' => 'ts',
                'sortOrder' => 'ASC',

            ];

            $output = $this->request('GET', $url, $params, $this->header);
            $output = json_decode($output, true);
            if (!empty($output)) {
                if (count($output) >= 1) {
                    $data = array_merge($data, $output);
                    $flag = true;
                }
            }
            $page++;
        } while ($flag);

        return $data;
    }


}